//
//  AppDelegate.swift
//  Weibo
//
//  Created by 伏文东 on 2018/1/22.
//  Copyright © 2018年 伏文东. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // 设置应用程序额外设置
        setupAdditions()
        
        window = UIWindow()
        window?.rootViewController = WBMainViewController()
        window?.makeKeyAndVisible()
        loadAppInfo()
        return true
    }
}

extension AppDelegate {
    private func setupAdditions()  {
        
        // 1. 设置 SVProguressHUD 最小解除时间
        SVProgressHUD.setMaximumDismissTimeInterval(1)
        
        // 2. 设置网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        // 取得用户授权显示通知[上方提示条/声音/ BadgeNumber ]
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .carPlay, .sound]) { (success, error) in
                print("授权" + (success ? "成功": "失败"))
            }
        } else {
            let notifySettings = UIUserNotificationSettings(types: [.alert,.badge, .sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifySettings)
            
        }
        
    }
}

extension AppDelegate {
    private func loadAppInfo() {
    
        DispatchQueue.global().async {
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            let data = NSData(contentsOf: url!)
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
            data?.write(toFile: jsonPath, atomically: true)
        }
    }
}

